package com.zh.config.rabbitmq;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


/**
 * Direct 死信队列
 */
@Component
public class DieDirectRabbitConfig {
    private  final static String dieRouting = "DeadRouting"; // 死信的routingkey
    private  final static String dieExchange= "DeadExchange"; // 死信的routingkey

    private final static String routing ="testDeadRouting"; // 正常的routing


    @Autowired
    RabbitAdmin rabbitAdmin;

        /**
         * Direct 队列
         * @return
         */
    @Bean("createDeadDirectQueue")
    public Queue createDeadDirectQueue(){
        // 定义死信队列
        Map<String,Object> params = new HashMap<>();
        params.put("x-dead-letter-exchange",dieExchange);
        params.put("x-dead-letter-routing-key",dieRouting);
//        params.put("x-message-ttl",120000); // 死信队列的生命周期
        // 参数说明： public Queue(String name, boolean durable, boolean exclusive, boolean autoDelete){}
        // name：     队列名称
        // durable：  是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive：默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete：默认为false,是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除
        return new Queue("TestDeadQueue",true,false,false,params);
    }

    /**
     * Direct 交换机
     * @return
     */
    @Bean("createDeadDirectExchange")
    public DirectExchange createDeadDirectExchange(){
        // 参数说明： public DirectExchange(String name, boolean durable, boolean autoDelete){}
        // name:    交换机名称
        // durable： 是否持久化
        // autoDelete： 是否自动删除
        return new DirectExchange("test-dead-exchange",true,false);
    }

    /**
     * 绑定  将队列和交换机绑定，并设置用于匹配键 routing
     * @return
     */
    @Bean
    public Binding createDeadDirectBinding(@Qualifier("createDeadDirectQueue") Queue queue,
                                           @Qualifier("createDeadDirectExchange") DirectExchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with(routing);
    }

    /**
     * 定义死信队列
     */
    @Bean("createDieQueue")
    public Queue createDieQueue(){
        return new Queue("DeadQueue",true);
    }

    /**
     * 定义死信的交换机
     * 这个交换机名称应该与上面正常队列绑定的死信队列交换机名称一样
     * params.put("x-dead-letter-exchange","DeadExchange");
     * @return
     */
    @Bean("createDieDirectExchange")
    public DirectExchange createDieDirectExchange(){
        return new DirectExchange(dieExchange,true,false);
    }

    /**
     * 绑定私信队列和死信交换机，routingkey 也使用上面的
     *  params.put(" x-dead-letter-routing-key",dieRouting);
     * @return
     */
    @Bean
    public Binding createDieDirectBinding(@Qualifier("createDieQueue") Queue queue,
                                          @Qualifier("createDieDirectExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(dieRouting);
    }

    //创建初始化RabbitAdmin对象
    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        // 只有设置为 true，spring 才会加载 RabbitAdmin 这个类
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

    /**
     * 使用依赖注入的方式注入bean
     * @Qualifier 的参数值就是 我们为bean起得名称
     */
    @Autowired
    @Qualifier("createDeadDirectExchange")
    private DirectExchange createDeadDirectExchange;

    @Autowired
    @Qualifier("createDieDirectExchange")
    private DirectExchange createDieDirectExchange;

    @Autowired
    @Qualifier("createDeadDirectQueue")
    private Queue createDeadDirectQueue;


    @Autowired
    @Qualifier("createDieQueue")
    private Queue createDieQueue;

    //启动自动创建交换机和对列
//    @Bean
//    public void createExchangeQueue () {
//        rabbitAdmin.declareExchange(createDeadDirectExchange);
//        rabbitAdmin.declareExchange(createDieDirectExchange);
//        rabbitAdmin.declareQueue(createDeadDirectQueue);
//        rabbitAdmin.declareQueue(createDieQueue);
//    }
}
